package com.taru.order.dao;

import com.taru.order.entity.Articl;
import com.taru.order.entity.Comment;
import com.taru.order.entity.User;
import com.taru.order.util.DbUtil;
import com.taru.order.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommentDaoimpl {
    /**
     * 对文章进行评论
     */
    public int commentArticl(Comment comment){
        String sql="insert into tb_comment(com_context,user_id,articl_id,parent_id,com_time) values (?,?,?,0,now())";
        int isn= DbUtil.update(sql,comment.getCom_context(),comment.getUser_id(),comment.getArticl_id());
        DbUtil.close();
        return isn;
    }
    /**
     * 对回复评论
     */
    public int replyComment(Comment comment){
        String sql="insert into tb_comment(com_context,user_id,articl_id,parent_id,com_time) values (?,?,?,?,now())";
        int isn= DbUtil.update(sql,comment.getCom_context(),comment.getUser_id(),comment.getArticl_id(),comment.getComment_id());
        DbUtil.close();
        return isn;
    }
    /**
     * 查询该篇文章的所有评论
     */
    public List<Comment> selectComment(String articl_id){
        String sql="select t1.*,t2.username,t2.header,t2.fans,t2.nickname from tb_comment t1  left outer join  tb_user t2 on t1.user_id=t2.user_id where articl_id=?";
        List<Map<String, Object>> listMap=DbUtil.query(sql,articl_id);
        List<Comment> comments = new ArrayList<Comment>();
        if(listMap!=null&&listMap.size()>0){
            for (Map<String, Object> map : listMap) {
                User user=new User();
                user.setUser_id(StringUtil.valueOf(map.get("user_id")));
                user.setUsername(StringUtil.valueOf(map.get("username")));
                user.setHeader(StringUtil.valueOf(map.get("header")));
                user.setFans(StringUtil.valueOf(map.get("fans")));
                user.setNickname(StringUtil.valueOf(map.get("nickname")));
                Comment comment = new Comment();
                comment.setComment_id(StringUtil.valueOf(map.get("comment_id")));
                comment.setCom_context(StringUtil.valueOf(map.get("com_context")));
                comment.setCom_time(StringUtil.valueOf(map.get("com_time")));
                comment.setUser_id(StringUtil.valueOf(map.get("user_id")));
                comment.setArticl_id(StringUtil.valueOf(map.get("articl_id")));
                comment.setCom_likes(StringUtil.valueOf(map.get("com_likes")));
                comment.setParent_id(StringUtil.valueOf(map.get("parent_id")));
                comment.setUser(user);
                comments.add(comment);
            }
        }
        DbUtil.close();
        return comments;
    }
    /**
     * 通过comment_id查询评论的回复
     */
    /*public List<Comment> selectReplay(String comment_id){
        String sql="select tb_comment.comment_id,tb_comment.com_time,tb_comment.user_id,tb_comment.com_context,tb_user.header,tb_user.nickname from tb_comment  left outer join  tb_user  on tb_comment.user_id=tb_user.user_id where  partent_id=?";
        List<Map<String, Object>> listMap=DbUtil.query(sql,comment_id);
        List<Comment> comments = new ArrayList<Comment>();
        if(listMap!=null&&listMap.size()>0){
            for (Map<String, Object> map : listMap) {
                Comment comment = new Comment();
                comment.setComment_id(StringUtil.valueOf(map.get("comment_id")));
                comment.setCom_context(StringUtil.valueOf(map.get("com_context")));
                comment.setCom_time(StringUtil.valueOf(map.get("com_time")));
                comment.setUser_id(StringUtil.valueOf(map.get("user_id")));
                comment.setArticl_id(StringUtil.valueOf(map.get("articl_id")));
                comment.setCom_likes(StringUtil.valueOf(map.get("com_likes")));
                comment.setParent_id(StringUtil.valueOf(map.get("parent_id")));
                comment.setNickname(StringUtil.valueOf(map.get("nickname")));
                comment.setHaeder(StringUtil.valueOf(map.get("header")));
                comments.add(comment);
            }
        }
        DbUtil.close();
        return comments;
    }*/

}
